<?php
namespace Home\Controller;

//通用Controller类
class CommonController extends \Think\Controller{
    
    public $uid; //当前用户id
    public $fromuser; //当前用户openid
    
    public function _initialize(){
        header("Content-Type: text/html; charset=utf-8");

        //获取用户fromuser
        $this->fromuser = session('fromuser');
        
        //便捷入口，携带fromuser参数可绕过oauth授权，生产环境需注释
        if(I('get.fromuser') && empty($this->fromuser)){
            session('fromuser',I('get.fromuser'));
            $this->fromuser = I('get.fromuser');
        }
        
        //oauth授权获取fromuser（即用户openid），并写session
        if(isset($_REQUEST['code']) && $this->fromuser==''){
            $this->fromuser = D('WxApi')->getAuthOpenid($_REQUEST['code'],C('APPID'),C('APPSECRET'));
            if(empty($this->fromuser)){
                die('<script>alert("获取用户标识失败")</script>');
            }else{
                session('fromuser',$this->fromuser);
            }
        }elseif($this->fromuser==''){
            $redirectUri="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];//openid处理页面(当前页面)
            D('WxApi')->getAuthCode($redirectUri,C('APPID'));
        }
        
        //用户信息判断
        $uRow = M('User')->field('id,type,fromuser,phone,name,hpt_id,card_num')->where(array('fromuser'=>$this->fromuser))->find();
        if(empty($uRow) || empty($uRow['phone'])){
            $this->redirect('User/phone');//所有用户-去完善手机
        }elseif($uRow['type']=='2' && empty($uRow['name'])){
            $this->redirect('User/complete');//患者-去完善姓名等
        }elseif($uRow['type']=='2' && $uRow['hpt_id'] != 1 && empty($uRow['card_num'])){
            $this->redirect('User/idcard');//患者-去完善身份证
        }else{
            $this->uid = $uRow['id'];
        }
    }
    
}